package cn.mcch.investigator.presenter.impl;

import android.text.TextUtils;

import java.util.LinkedHashMap;
import java.util.Map;

import cn.mcch.investigator.R;
import cn.mcch.investigator.application.InvestigatorApplication;
import cn.mcch.investigator.listener.OnResponseListener;
import cn.mcch.investigator.model.impl.UpdatePasswordModelImpl;
import cn.mcch.investigator.presenter.BasePresenter;
import cn.mcch.investigator.presenter.IUpdatePasswordPresenter;
import cn.mcch.investigator.utils.ConstantUtil;
import cn.mcch.investigator.utils.ToastUtils;
import cn.mcch.investigator.viewdelegate.impl.UpdatePasswordViewDelegateImpl;

/**
 * Created by Administrator on 2016/12/17.
 */
public class UpdatePasswordPresenterImpl extends BasePresenter<UpdatePasswordViewDelegateImpl, UpdatePasswordModelImpl> implements IUpdatePasswordPresenter {
    @Override
    public Class<UpdatePasswordViewDelegateImpl> getViewDelegateClass() {
        return UpdatePasswordViewDelegateImpl.class;
    }

    @Override
    public Class<UpdatePasswordModelImpl> getModelClass() {
        return UpdatePasswordModelImpl.class;
    }


    @Override
    public boolean verify() {
        String oldPass = mViewDelegate.getText(R.id.se_old_password);
        String newPass = mViewDelegate.getText(R.id.se_new_password);
        String newAgainPass = mViewDelegate.getText(R.id.se_new_password_again);

        if (TextUtils.isEmpty(oldPass)) {
            ToastUtils.showToast("原密码不能为空");
            return false;
        }
        if (TextUtils.isEmpty(newPass)) {
            ToastUtils.showToast("新密码不能为空");
            return false;
        }
        if (TextUtils.isEmpty(newAgainPass)) {
            ToastUtils.showToast("确认密码不能为空");
            return false;
        }
        if (!newPass.equals(newAgainPass)) {
            ToastUtils.showToast("新密码和确认密码不相同，请检查");
            return false;
        }
        if (newPass.equals(oldPass)) {
            ToastUtils.showToast("新密码不可以和旧密码相同，请检查");
            return false;
        }

        return true;
    }

    @Override
    public void updatePassword(int id, OnResponseListener l) {
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("user_id", InvestigatorApplication.getInstance().getUserBeanbean().user_id);
        params.put("old_pwd", mViewDelegate.getText(R.id.se_old_password));
        params.put("new_pwd", mViewDelegate.getText(R.id.se_new_password));
        params.put("re_pwd", mViewDelegate.getText(R.id.se_new_password_again));
        mModel.updatePassword(id, ConstantUtil.ACT_UPDATE_PASSWORD, params, l);
    }
}
